package com.fengyunhe.manager;

import com.fengyunhe.common.RequestThread;
import com.fengyunhe.common.AppContext;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.servlet.support.RequestContextUtils;

import java.io.Serializable;
import java.util.Locale;

/**
 * 功能：当前用户
 * 作者： yangyan
 * 时间： 2014/9/5 .
 */
public class CurrentUser implements Serializable {

    private Integer userId;
    private String realName;
    private String displayName;
    private String loginIp;
    private Object userType;

    public String getRealName() {
        return realName;

    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getLoginIp() {
        return loginIp;
    }

    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }

    public Object getUserType() {
        return userType;
    }

    public void setUserType(Object userType) {
        this.userType = userType;
    }


    /**
     * 获取当前登录的用户，用户的信息来自与shiro管理的session中，session 的key，取自AppContext.getAdminSessionKey()
     *
     * @return
     */
    public static CurrentUser get() {
        Object user = SecurityUtils.getSubject().getSession(true).getAttribute(AppContext.getAdminSessionKey());
        if (user != null) {
            return (CurrentUser) user;
        }
        return null;
    }

    /**
     * 获取用户当前的设置的语言，只能在使用spring mvc request 请求时使用，请勿在其他情况下调用
     *
     * @return
     */
    public static Locale getLocale() {
        return RequestContextUtils.getLocale(RequestThread.getRequest());
    }
}
